package com.smartlifev30.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.smartlifev30.baiwei.App;
import com.smartlifev30.utils.NetUtils;

import static com.smartlifev30.utils.NetUtils.getCurrentNetStatus;

/**
 * @author tys
 * @date 2018/8/5
 */

public class NetChangeObservable {
    private static NetChangeCallback netChangeCallback;
    private static V24.NetworkCallback v24NetworkCallback;

    private static void notifyObservers(int status) {
        if (null != netChangeCallback) {
            netChangeCallback.netChange(status);
        }
    }

    /**
     * 注册静态广播{@link NetChangeObservable.V14}
     * 应用在后台也会收到相应的通知
     *
     * @param netChangeCallback
     */
    public static void register(NetChangeCallback netChangeCallback) {
        NetChangeObservable.netChangeCallback = netChangeCallback;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            getNetStatusAndNotify(App.getInstance());
            //7.0下 请在AndroidManifest中注册静态广播
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            getNetStatusAndNotify(App.getInstance());
            //注册网络回调(android.Manifest.permission.ACCESS_NETWORK_STATE)
            v24NetworkCallback = new V24.NetworkCallback();
            V24.registerCallback(App.getInstance(), v24NetworkCallback);
        }
    }

    public static void unregister() {
        NetChangeObservable.netChangeCallback = null;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            //7.0下静态广播
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            if (v24NetworkCallback != null) {
                V24.unregister(App.getInstance(), v24NetworkCallback);
            }
        }
        v24NetworkCallback = null;
    }

    static class V24 {
        @RequiresApi(api = Build.VERSION_CODES.N)
        static void registerCallback(Context context, ConnectivityManager.NetworkCallback callback) {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (cm != null) {
                cm.registerDefaultNetworkCallback(callback);
            }
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        static void unregister(Context context, ConnectivityManager.NetworkCallback callback) {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (cm != null) {
                cm.unregisterNetworkCallback(callback);
            }
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        static class NetworkCallback extends ConnectivityManager.NetworkCallback {

            @Override
            public void onAvailable(Network network) {
                //某种网络连接(比如是wifi 或者 MOBILE連接)
                // mobile -> wifi  中间没有断开过程
                // wifi -> mobile  中间会断开一下
                Log.e("0-0-", "onAvailable");
                int netStatus = NetUtils.getCurrentNetStatus(App.getInstance());
                notifyObservers(netStatus);
            }

            @Override
            public void onLost(Network network) {
                //某种网络断开 连接(比如是wifi 或者 MOBILE 断开了连接)
                Log.e("0-0-", "onLost");
                int netStatus = NetUtils.getCurrentNetStatus(App.getInstance());
                notifyObservers(netStatus);
            }
        }
    }

    public static class V14 extends BroadcastReceiver {
        public V14() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("0-0-", "广播网络变化");
            if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
                getNetStatusAndNotify(context);
            }
        }
    }


    private static void getNetStatusAndNotify(Context applicationContext) {
        int currentNetStatus = NetUtils.getCurrentNetStatus(applicationContext);
        notifyObservers(currentNetStatus);
    }

    public interface NetChangeCallback {

        /**
         * {@link NetworkInfo#getType()}
         *
         * @param status 当前网络类型
         */
        void netChange(int status);
    }

}



